//
//  NumberView.m
//  xpkc
//
//  Created by TopSageOSX on 15/1/7.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "NumberView.h"

@implementation NumberView{
    NSMutableArray *numArray;
    NSMutableArray *viewArray;
    UILabel *symbol;
    UIScrollView *Yi;
    UIScrollView *QWan;
    UIScrollView *BWan;
    UIScrollView *SWan;
    UIScrollView *Wan;
    UIScrollView *Qian;
    UIScrollView *Bai;
    UIScrollView *Shi;
    UIScrollView *Ge;
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.number = 0;
        [self setNumber:0];
        [self creatNumberView];
    }
    return self;
}

- (void)creatNumberView{
    viewArray = [NSMutableArray array];
    symbol = [[UILabel alloc]init];
    Wan = [[UIScrollView alloc]init];
    Qian = [[UIScrollView alloc]init];
    Bai = [[UIScrollView alloc]init];
    Shi = [[UIScrollView alloc]init];
    Ge = [[UIScrollView alloc]init];
    [viewArray insertObject:Ge atIndex:0];
    [viewArray insertObject:Shi atIndex:0];
    [viewArray insertObject:Bai atIndex:0];
    [viewArray insertObject:Qian atIndex:0];
    [viewArray insertObject:Wan atIndex:0];
    CGFloat height = self.frame.size.height;
    symbol.frame = CGRectMake(0, 0, 5, height);
    int j = 0;
    for (UIScrollView *view in viewArray) {
        view.frame = CGRectMake(j*10, 0, 10, height);
        view.contentSize = CGSizeMake(10, height*10);
        j++;
        for (int i = 0; i<=9; i++) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, i*self.frame.size.height, 10, height)];
            label.text = [NSString stringWithFormat:@"%d",9-i];
            [view addSubview:label];
        }
    }
    
    symbol.text = @"-";
    
    symbol.hidden = YES;
    
    [self addSubview:symbol];
    [self addSubview:Wan];
    [self addSubview:Qian];
    [self addSubview:Bai];
    [self addSubview:Shi];
    [self addSubview:Ge];
    
}

- (int)number{
    return self.number;
}

- (void)setFont:(UIFont *)font{
    for (UIScrollView *scroll in viewArray) {
        for (UILabel *numLabel in scroll.subviews) {
            numLabel.font = font;
        }
    }
    
}
- (void)setColor:(UIColor *)color{
    for (UIScrollView *scroll in viewArray) {
        for (UILabel *numLabel in scroll.subviews) {
            numLabel.textColor = color;
        }
    }
}

- (void)setNumber:(int)number{
    int num = number;
    numArray = [NSMutableArray array];
    do {
        NSString *string = [NSString stringWithFormat:@"%d",num%10];
        [numArray insertObject:string atIndex:0];
        num = num/10;
    } while (num != 0);
}

- (void)setNumerTo:(int)num animation:(BOOL)animation{
    [self setNumber:num];
    for (int i = viewArray.count-1; i>=0; i--) {
        UIScrollView *scroll = [viewArray objectAtIndex:i];
        int index = numArray.count - (viewArray.count - i);
        if (index>=0) {
            int n = [[numArray objectAtIndex:index]intValue];
            scroll.hidden = NO;
            if (animation) {
                [UIView animateWithDuration:0.3 animations:^{
                    scroll.contentOffset = CGPointMake(0, (9-n)*self.frame.size.height);
                }];
            }else{
                scroll.contentOffset = CGPointMake(0, (9-n)*self.frame.size.height);
            }
        }else{
            scroll.hidden = YES;
        }
    }
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    //重置位置
    CGFloat height = frame.size.height;
    for (UIScrollView *scroll in viewArray) {
        for (int i = 0; i<scroll.subviews.count; i++) {
            UILabel *label = [scroll.subviews objectAtIndex:i];
            label.top = i*height;
            label.height = height;
        }
    }
}

@end
